﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Dapper.Database;
using Dapper.Database.Extensions;

namespace QuickFramework.Orm
{
    public partial class Repository : IRepository
    {
        /// <summary>
        /// 数据库链接节点名称
        /// </summary>
        private string m_ConnectionStringName;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectionStringName"></param>
        public Repository(string connectionStringName)
        {
            m_ConnectionStringName = connectionStringName;
        }
        /// <summary>
        /// 仓储内部使用的工作单元
        /// </summary>
        public IUnitOfWork UnitOfWork
        {
            get { return new UnitOfWork().CreateOpenConnection(m_ConnectionStringName); }
        }
        /// <summary>
        /// 获取数据库表名
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private string GetTableName<T>() where T : Shared.Contracts.EntityBase
        {
            return typeof(T).Name;
        }
        /// <summary>
        /// 更新数据,只更新变更过值的属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entityToUpdate"></param>
        /// <param name="unitOfWork"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        public bool Update4Setting<T>(T entityToUpdate, IUnitOfWork unitOfWork = null, int? commandTimeout = null)
            where T : Shared.Contracts.EntityBase
        {
            //先判断主键是否进行更新过.如果更新.则创建SQL语句进行处理
            if (entityToUpdate.PKPropertyChanges.Count > 0)
            {
                throw new Exception("不支持主键更新");
            }
            if (unitOfWork != null)
            {
                return unitOfWork.Connection.Update<T>(entityToUpdate, entityToUpdate.PropertyChanges, unitOfWork.Transaction, commandTimeout);
            }
            else
            {
                using (var uow = UnitOfWork)
                {
                    return uow.Connection.Update<T>(entityToUpdate, entityToUpdate.PropertyChanges, uow.Transaction, commandTimeout);
                }
            }
        }
        /// <summary>
        /// 更新数据,只更新变更过值的属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entityToUpdate"></param>
        /// <param name="unitOfWork"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        public Task<bool> Update4SettingAsync<T>(T entityToUpdate, IUnitOfWork unitOfWork = null, int? commandTimeout = null)
            where T : Shared.Contracts.EntityBase
        {
            //先判断主键是否进行更新过.如果更新.则创建SQL语句进行处理
            if (entityToUpdate.PKPropertyChanges.Count > 0)
            {
                throw new Exception("不支持主键更新");
            }
            if (unitOfWork != null)
            {
                return unitOfWork.Connection.UpdateAsync<T>(entityToUpdate, entityToUpdate.PropertyChanges, unitOfWork.Transaction, commandTimeout);
            }
            else
            {
                using (var uow = UnitOfWork)
                {
                    return uow.Connection.UpdateAsync<T>(entityToUpdate, entityToUpdate.PropertyChanges, uow.Transaction, commandTimeout);
                }
            }
        }

    }
}
